import { ref } from 'vue'
import { ConfigClass } from './config'
import { TypeManageApi } from './api'
import { ElMessage } from 'element-plus'
export class TypeManageClass {
	configClass: ConfigClass
	baseData = ref()
	total = ref(0)
	public query = ref()
	visible = ref<boolean>(false)
	defaultValue = ref()
	commonForm
	constructor(commonForm) {
		this.commonForm = commonForm
		this.configClass = new ConfigClass(this)
		this.getList()
	}

	/**
	 * 获取表格配置
	 */
	public getTableConfig() {
		return { ...this.configClass.tableConfig, data: this.baseData.value, total: this.total.value }
	}

	/**
	 * 查询配置
	 */
	public getSearchCol() {
		return this.configClass.searchCol
	}

	/**
	 * 查询
	 */
	public serach(query) {
		this.query.value = query
		this.configClass.tableConfig.pagination.currentPage = 1
		this.getList()
	}
	/**
	 * 获取表格数据
	 */
	public getList() {
		const params = {
			...this.query.value,
			currentPage: this.configClass.tableConfig.pagination.currentPage,
			pageSize: this.configClass.tableConfig.pagination.pageSize
		}
		TypeManageApi.getTable(params).subscribe((res: any) => {
			const { records = [], total = 0 } = res
			this.baseData.value = records
			this.total.value = total
		})
	}
	/**
	 * 删除
	 */
	public delete(id: number) {
		const params = {
			id: id
		}
		TypeManageApi.delete(params).subscribe(() => {
			ElMessage.success('删除成功')
			this.getList()
		})
	}

	/**
	 * 编辑
	 */
	public edit(row: any) {
		this.defaultValue.value = { ...row }
		this.visible.value = true
	}

	/**
	 *
	 */
	public getFormCol() {
		return this.configClass.formCol
	}

	/**
	 * 编辑按钮
	 */
	public btns() {
		return this.configClass.btns
	}

	/**
	 * 编辑信息
	 */
	public confirm() {
		this.commonForm.value.validForm().then(() => {
			const formData = this.commonForm.value.getFormData()
			const params = {
				...formData
			}
			TypeManageApi.addType(params).subscribe(() => {
				ElMessage.success('新增成功')
				this.visible.value = false
				this.getList()
			})
		})
	}

	/**
	 * 新增
	 */
	public add() {
		this.visible.value = true
	}

	public onPageChange() {
		this.getList()
	}
}
